package com.example.androidtest1;

import com.example.aidl.IJiniService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AIDLTestActivity extends Activity implements OnClickListener {

	Button btnBind;
	Button btnCall;
	TextView valueTex;
	IJiniService jiniService;

	private ServiceConnection serviceConn = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName arg0) {

		}

		@Override
		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
			jiniService = IJiniService.Stub.asInterface(arg1);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aidltest);

		btnBind = (Button) findViewById(R.id.btnBind);
		btnCall = (Button) findViewById(R.id.btnCall);
		valueTex = (TextView) findViewById(R.id.textView1);
		
		btnBind.setOnClickListener(this);
		btnCall.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.aidltest, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBind:
			bindService(new Intent("com.jinisu.test.service.JINISERVICE1"),
					serviceConn, Context.BIND_AUTO_CREATE);
			break;
		case R.id.btnCall:
			try {
				valueTex.setText(jiniService.getValue());
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			break;

		default:
			break;
		}
	}

}
